package com.captain.leetcode.排序算法;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/3/20 16:54
 */
public class BubbleSort {
    public static void main(String[] args) {
        for (int i : new BubbleSort().bubbleSort(new int[]{5, 4, 3, 2, 1}, 5)) {
            System.out.println(i);
        }

    }

    public int[] bubbleSort(int[] a, int n) {
        //123456
        if (n < 1) return a;
        for (int i = 1; i < n; i++) {
            boolean flag = false;
            for (int j = 0; j < n - i; j++) {
                //前面的比后面的大, 交换位置
                if (a[j] > a[j + 1]) {
                    int tp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = tp;
                    flag = true;
                }
            }
            if (!flag) break;
        }
        return a;
    }
}
